package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.FitmentBean;
import com.wnhz.work.yz.bean.MessageBean2;
import com.wnhz.work.yz.view.RoundImageView;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by admin on 2017/3/3.
 */
public class FitmentAdapter extends BaseAdapter {
    private Context context;
    private View.OnClickListener listener;
    private List<FitmentBean> dataList;

    public FitmentAdapter(Context context, View.OnClickListener listener, List<FitmentBean> dataList) {
        this.context = context;
        this.listener = listener;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private boolean flag = true;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ProjectShopViewHolder viewHolder;
        Log.e("----", position + "");
        if (convertView == null) {
            viewHolder = new ProjectShopViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shopping_caet, null);
            viewHolder.select = (ImageView) convertView.findViewById(R.id.select);
            viewHolder.img = (RoundImageView) convertView.findViewById(R.id.img);
            viewHolder.reduce = (ImageView) convertView.findViewById(R.id.reduce);
            viewHolder.ll_add = (ImageView) convertView.findViewById(R.id.ll_add);
            viewHolder.shopping_number = (TextView) convertView.findViewById(R.id.shopping_number);
            viewHolder.residue_number_tv = (TextView) convertView.findViewById(R.id.residue_number_tv);
            viewHolder.title_tv = (TextView) convertView.findViewById(R.id.title_tv);
            viewHolder.material_tv = (TextView) convertView.findViewById(R.id.material_tv);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.standard_tv = (TextView) convertView.findViewById(R.id.standard_tv);
            viewHolder.select_multiple = (LinearLayout) convertView.findViewById(R.id.select_multiple);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ProjectShopViewHolder) convertView.getTag();
        }

        FitmentBean bean1 = dataList.get(position);
        if (bean1.isselect()) {
            viewHolder.select.setImageResource(R.mipmap.ic_choose_li);
        } else {
            viewHolder.select.setImageResource(R.mipmap.ic_choose_nor);
        }
        viewHolder.ll_add.setImageResource(R.mipmap.ic_add_li);
        viewHolder.reduce.setImageResource(R.mipmap.ic_decrease_li);
        viewHolder.residue_number_tv.setText(bean1.getStore() + " 件商品");
        viewHolder.title_tv.setText(bean1.getGoods_name());
        DecimalFormat df = new DecimalFormat("######0.00");
        Log.e("价格", bean1.getOprice()+"    "+bean1.getOprice());
        try {

            viewHolder.tv_price.setText("¥ " + df.format(Double.parseDouble(bean1.getOprice())));
//             * Double.parseDouble(MyApplication.getInstance().getUserBean().getRatio().toString())
        } catch (Exception e) {
            viewHolder.tv_price.setText("¥ " + df.format(Double.parseDouble(bean1.getOprice())));
        }
        viewHolder.material_tv.setText(bean1.getWeight());
        if (!"".equals(bean1.getSku_des()) && bean1.getSku_des() != null) {
            viewHolder.standard_tv.setText(bean1.getSku_des());
            viewHolder.select_multiple.setVisibility(View.VISIBLE);
        } else {
            viewHolder.select_multiple.setVisibility(View.GONE);
        }
        x.image().bind(viewHolder.img, bean1.getLogo_pic());
        viewHolder.shopping_number.setText(dataList.get(position).getNum());
        viewHolder.shopping_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View view = View.inflate(context, R.layout.dialog_edit, null);
                TextView queding1 = (TextView) view.findViewById(R.id.queding1);
                TextView quxaio1 = (TextView) view.findViewById(R.id.quxiao1);
                final EditText shopping_number = (EditText) view.findViewById(R.id.shopping_number);
                shopping_number.setText(dataList.get(position).getNum());
                builder.setView(view);
                final AlertDialog show = builder.show();
                Window dialogWindow = show.getWindow();
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                dialogWindow.setGravity(Gravity.CENTER);
                dialogWindow.setAttributes(lp);
                queding1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int nums = Integer.parseInt(dataList.get(position).getNum());
                        try {
                            nums = Integer.parseInt(shopping_number.getText().toString());
                        } catch (Exception e) {
                            nums = Integer.parseInt(dataList.get(position).getNum());
                        }
                        if (nums == 0) {
                            nums = 1;
                            Toast.makeText(context, "购买数量不能小于1", Toast.LENGTH_SHORT).show();
                        }
                        if (nums > Integer.parseInt(dataList.get(position).getStore())) {
                            Toast.makeText(context, "库存不足，已转为最大值", Toast.LENGTH_SHORT).show();
                            nums = Integer.parseInt(dataList.get(position).getStore());
                        }
                        dataList.get(position).setNum(nums + "");
                        MessageBean2 event = new MessageBean2();
                        event.setIndex(position);
                        event.setNums(nums);

                        viewHolder.shopping_number.setText(nums + "");
                        EventBus.getDefault().post(event);
                        show.dismiss();
                    }
                });
                quxaio1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        show.dismiss();
                    }
                });
            }
        });

        viewHolder.select.setOnClickListener(listener);
        viewHolder.select.setTag(position);
        viewHolder.reduce.setOnClickListener(listener);
        viewHolder.reduce.setTag(position);
        viewHolder.reduce.setTag(R.id.add111, viewHolder.shopping_number);
        viewHolder.ll_add.setOnClickListener(listener);
        viewHolder.ll_add.setTag(position);
        viewHolder.ll_add.setTag(R.id.add111, viewHolder.shopping_number);

        return convertView;
    }

    private int numS = 1;

    class ProjectShopViewHolder {
        RoundImageView img;
        ImageView select, reduce, ll_add;
        TextView shopping_number;
        TextView residue_number_tv, title_tv, tv_price, material_tv, standard_tv;
        LinearLayout select_multiple;
    }
}